<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchQuery" class="demo-form-inline">
      <el-form-item label="课程类别" >
        <el-select placeholder="请选择"
         v-model="searchQuery.subjectParentId"
         @change="subjectLevelOneChange"
         >
          <el-option
            v-for="item in subjectNestedList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select placeholder="请选择" v-model="searchQuery.subjectId">
        <el-option
            v-for="item in subSubjectList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input placeholder="课程标题" v-model="searchQuery.title"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select placeholder="请选择讲师" v-model="searchQuery.teacherId">
            <el-option
            v-for="item in teacherList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
          </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getList()"
          >查询</el-button
        >
        <el-button @click="resetData()">清空</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      row-class-name="myClassList"
    >
      <el-table-column align="center" label="序号" width="70">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="课程信息" width="300">
        <template slot-scope="scope">
          <div class="info">
            <div class="pic">
              <img :src="scope.row.cover" :alt="scope.row.title" />
            </div>
            <div class="title">
              <a href="#">{{ scope.row.title }}</a>
              <p>{{ scope.row.lessonNum }}课时</p>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.gmtModified.substr(0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.gmtCreate.substr(0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格" width="100" align="center">
        <template slot-scope="scope">
          <span>{{
            scope.row.price == 0 ? "免费" : "￥" + scope.row.price.toFixed(2)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付费学员" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.buyCount }}人</span>
        </template>
      </el-table-column>
      <el-table-column label="课程状态" width="100" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 'Normal' ? 'success' : ''">
            {{ scope.row.status == "Normal" ? "已发布" : "未发布" }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="`/coures/info/${scope.row.id}`">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >修改</el-button
            >
          </router-link>

          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteCourse(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      style="text-align: center; margin-top: 20px"
      @current-change="getList"
      :current-page="page"
      :page-size="limit"
      layout="total, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
import teacherApi from "@/api/teacher";
import couresApi from "@/api/coures";
import subjectAPI from '@/api/subject'
export default {
  data() {
    return {
      list: null, //当前列表所需数据
      listLoading: true, //转菊花效果
      page: 1, //当前页
      limit: 10, //每页多少数据
      searchQuery: {
        teacherId:'',//讲师id
        subjectId:'',//课程分类id
        subjectParentId:'',//一级课程分类id
        title:''//课程标题
      }, //条件查询对象
      total: 0, //总条数
      subjectNestedList:[],//一级课程分类
      teacherList:[],//讲师数据
      subSubjectList:[]//二级课程分类
    };
  },
  created() {
    // 获取课程列表分页数据
    this.getList();

    // 获取一级课程分类
    this.initSubjectList()

    // 获取讲师基本信息
    this.initTeacherList()
  },
  methods: {
    getList(page = 1) {
      // console.log(page)
      // 获取当前页码
      this.page = page;

      //调用讲师列表Api
      this.listLoading = true;
      couresApi.getList(this.page, this.limit, this.searchQuery).then((res) => {
        // 获取API数据
        this.list = res.data.items;

        // 获取总条数
        this.total = res.data.totalCount;
        // 让转菊花效果停止
        this.listLoading = false;
      });
    },
    // 清空查询数据
    resetData() {
      // 将查询表单对象内容清空
      this.searchQuery = {};
      // 调用讲师API接口
      this.getList();
    },
    // 初始化课程分类下拉框
      initSubjectList(){
        // 调用课程分类API
        subjectAPI.getList().then(res=>{
          this.subjectNestedList=res.data
        })
      },
      // 初始化讲师下拉框
      initTeacherList(){
        teacherApi.listTeacher().then(res=>{
          this.teacherList=res.data
        })
      },
      subjectLevelOneChange(value){
        console.log(value)
        // 根据当前一级分类，查询二级分类
        // 找到当前一级分类id下的children
        for(let i=0;i<this.subjectNestedList.length;i++){
          if(this.subjectNestedList[i].id==value){
            // 获取子集
            this.subSubjectList=this.subjectNestedList[i].children
            // 清空查询条件中的二级分类id
            this.subjectNestedList.subjectId=""
          }
        }
      },
    // 根据id删除课程
    deleteCourse(id) {
      // console.log(id)
      this.$confirm("您确定要删除该课程吗?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 调用删除课程API
          couresApi.deleteCourseBuid(id).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style scoped>
.myClassList .info {
  width: 300px;
  overflow: hidden;
}
.myClassList .info .pic {
  width: 150px;
  height: 90px;
  overflow: hidden;
  float: left;
}
.myClassList .info .pic a {
  display: block;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}
.myClassList .info .pic img {
  display: block;
  width: 100%;
}
.myClassList td .info .title {
  float: left;
  margin-left: 20px;
  height: 90px;
}
.myClassList td .info .title a {
  display: block;
  height: 48px;
  line-height: 24px;
  overflow: hidden;
  color: #00baf2;
  margin-bottom: 12px;
}
.myClassList td .info .title p {
  line-height: 20px;
  margin-top: 5px;
  color: #818181;
}
</style>